/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.util.ArrayList;
import java.util.List;

import who.tmap.bo.AttrDef;
import who.tmap.bo.Attrs;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.common.TMapException;
import who.tmap.toolkit.impl.AttrHelper;

/**
 * A helper for editing data structure
 * 
 * @see JDialogEditDataStructure 
 * @see EditDataStructureTableModel
 * @author stl
 *
 */
public class EditDataStructureHelper {

	public List<DataStructureColumnInfo> processDscis( List<AttrDef> attrDefs ) {
		List<DataStructureColumnInfo> dscis =new ArrayList<DataStructureColumnInfo>();
		for ( AttrDef attrDef : attrDefs ) {
			dscis.add( new DataStructureColumnInfo( attrDef ) );
		}
		
		return dscis;
	}
	
	@SuppressWarnings("unchecked")
	public void processAttrs( Attrs attrs, List<DataStructureColumnInfo> dscis ) throws TMapException {
		List<AttrDef> attrDefs =attrs.getHeader();
		List<List> rows =attrs.getRows();
		AttrHelper attrHelper =new AttrHelper();
		
		for( DataStructureColumnInfo dsci : dscis ) {

			// Testing order is important
			if ( dsci.isDeleted() ) {
				// STL 2007.06.08
				// Leave this test as is, don't do dsci.isDeleted() && !dsci.isNeww()
				if (!dsci.isNeww() ) {
					int idx =attrHelper.getIdxForAttrName( attrs.getHeader(), dsci.getOriginalName() );
					attrDefs.remove( idx );
					
					for( List row : rows ) {
						row.remove( idx );
					}
				}
			} else if ( dsci.isNeww() ) {
				AttrDef attrDef =(AttrDef)BizObjectFactory.getInstance().get( BizObjectFactory.ATTR_DEF );
				attrDef.setName( dsci.getUpdatedName() );
				attrDef.setType( dsci.getUpdatedType() );
				
				attrDefs.add( attrDef );
				
				for( List row : rows ) {
					row.add( attrHelper.getNewInstanceFromClassName( attrDef.getType() ) );
				}				
			} else if ( dsci.isUpdated() ) {
				int idx =attrHelper.getIdxForAttrName( attrs.getHeader(), dsci.getOriginalName() );

				dsci.getAttrDef().setName( dsci.getUpdatedName() );
				dsci.getAttrDef().setType( dsci.getUpdatedType() );
				
				if ( dsci.getOriginalType().equals( dsci.getUpdatedType() ) ) {
					for( List row : rows ) {
						Object val =row.get( idx );
						if ( val != null ) {
							Object convertedVal =attrHelper.getValueFromString( val.toString(), dsci.getUpdatedType() );
							row.set( idx, convertedVal );
						}
					}
				}
			}
		}
	}
	
}
